﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace IlianaRuler.Utils
{
    static class Utility
    {
        /// <summary>
        /// 获得数据的MD5值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] MD5Encrypt(byte[] data)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            return md5Hasher.ComputeHash(data);
        }

        /// <summary>
        /// AES算法的初始化向量
        /// </summary>
        private static byte[] AESIV = new byte[16] { 0x0f, 0x37, 0x8d, 0x2f, 0x4a, 0x04, 0x82, 0x58, 
                                                     0x9f, 0xee, 0x10, 0x00, 0x54, 0x1b, 0xc0, 0x4c };
        /// <summary>
        /// AES算法加密
        /// </summary>
        /// <param name="key"></param>
        /// <param name="originalData"></param>
        /// <returns></returns>
        public static byte[] AESEncrypt(byte[] key, byte[] data)
        {
            Rijndael aes = Rijndael.Create();
            ICryptoTransform transform = aes.CreateEncryptor(key, AESIV);
            return transform.TransformFinalBlock(data, 0, data.Length);
        }

        /// <summary>
        /// AES解密算法
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] AESDecrypt(byte[] key, byte[] data)
        {
            Rijndael aes = Rijndael.Create();
            ICryptoTransform transform = aes.CreateDecryptor(key, AESIV);
            return transform.TransformFinalBlock(data, 0, data.Length);
        }

        /// <summary>
        /// 调试信息输出
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            Console.WriteLine(String.Format("{0} DEBUG {1}", DateTime.Now, message));
        }

        /// <summary>
        /// 错误消息输出
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            Console.WriteLine(String.Format("{0} ERROR {1}", DateTime.Now, message));
        }

        /// <summary>
        /// 提示信息输出
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            Console.WriteLine(String.Format("{0} INFO {1}", DateTime.Now, message));
        }

        /// <summary>
        /// 警告信息输出
        /// </summary>
        /// <param name="message"></param>
        public static void Warning(string message)
        {
            Console.WriteLine(String.Format("{0} WARNING {1}", DateTime.Now, message));
        }
    }
}
